local addonName,ns = ...
local L = ns.L
local DB = ns.PlateColorDB
local SetLoaded--设置加载一次
ns.event("PLAYER_ENTERING_WORLD", function()
if SetLoaded then return end
SetLoaded = true
--分页4滚动内容不需要滚动框架就直接锚定在ns.tabframe4
local ConFrame4 = CreateFrame("Frame", nil, ns.tabframe4)
ConFrame4:SetSize(670,480)
ConFrame4:SetAllPoints(ns.tabframe4)
ns.Y[4] = 0	--设置起始位置

local auraltext = ns.AddSetTiText(ConFrame4,4,"")
local AuraDesc1 = ConFrame4:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
AuraDesc1:SetPoint("TOPLEFT",ConFrame4,"TOPLEFT", 15, -45)
AuraDesc1:SetFont(ns.fonts, 15, "OUTLINE")
AuraDesc1:SetTextColor(1,1,1)
AuraDesc1:SetText(L["光环说明一"])
local AuraDesc2 = ConFrame4:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
AuraDesc2:SetPoint("TOPLEFT",AuraDesc1,"BOTTOMLEFT", 0, -10)
AuraDesc2:SetFont(ns.fonts, 15, "OUTLINE")
AuraDesc2:SetTextColor(1,1,1)
AuraDesc2:SetText(L["光环说明二"])


--创建光环表格背景
local AuraTableFrame = CreateFrame("Frame", "MyTableFrame", ConFrame4, "BackdropTemplate")
AuraTableFrame:SetSize(650, 400)
AuraTableFrame:SetPoint("TOPLEFT",15,-90)
AuraTableFrame:SetBackdrop({
    bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
    edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
    edgeSize = 16,
    insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
AuraTableFrame:SetBackdropColor(0, 0, 0, 0.5)
AuraTableFrame:SetBackdropBorderColor(0.7, 0.7, 0.7)

-- 创建光环表格滚动框架
local AurascrollFrame = CreateFrame("ScrollFrame", "MyScrollFrame", AuraTableFrame, "UIPanelScrollFrameTemplate")
AurascrollFrame:SetPoint("TOPLEFT", AuraTableFrame, "TOPLEFT", 4, -5)
AurascrollFrame:SetPoint("BOTTOMRIGHT", AuraTableFrame, "BOTTOMRIGHT", -30, 5)

-- 创建光环内容框架
local AuracontentFrame = CreateFrame("Frame", nil, AurascrollFrame)
AuracontentFrame:SetSize(620, 180)
AurascrollFrame:SetScrollChild(AuracontentFrame)	--设置滚动框内容为内容框架

--自动同步缺少的光环
for id, autodatacon in pairs(DB["Auras"]) do
	if not PlateColorDB["Auras"][id] then
		PlateColorDB["Auras"][id] = autodatacon
	end
end
--自动删除多余的光环
for id, autodatacon in pairs(PlateColorDB["Auras"]) do
	if type(PlateColorDB["Auras"][id]) == "table" then
		if not DB["Auras"][id] and not PlateColorDB["Auras"][id]["set"] then
			PlateColorDB["Auras"][id] = nil
		end
		if DB["Auras"][id] and not PlateColorDB["Auras"][id]["set"] then
			PlateColorDB["Auras"][id] = DB["Auras"][id]
		end
	end
end

--初始化光环图表内容
for key, datacon in pairs(PlateColorDB["Auras"]) do
	if type(PlateColorDB["Auras"][key]) == "table" then
		local idname = ns.GetSpellName(key) or L["初始化光环名称错误"]
		if not datacon.color or datacon.color == 1 then
			PlateColorDB["Auras"][key]["color"] = {r=0,g=1, b=0}
		end
		ns.AddAuraTableButtons(AuracontentFrame,key,idname)
	end
end

--是否过滤默认
local function AuraTableHide()
	ns.AurayOffset = -5
	for key, row in pairs(ns.AurarowFrames) do
		if DB["Auras"][key] then
			if not PlateColorDB.auraShowDefault then
				ns.AurarowFrames[key]:Hide()
			else
				ns.AurarowFrames[key]:Show()
			end
		else
			ns.AurarowFrames[key]:Show()
		end
		if ns.AurarowFrames[key]:IsShown() then -- 只对显示的行重新定位
			ns.AurarowFrames[key]:SetPoint("TOPLEFT", 5, ns.AurayOffset)
			ns.AurayOffset = ns.AurayOffset - 28
		end
	end
end

--显示隐藏默认
local tableshown = CreateFrame("CheckButton", nil, ConFrame4, "InterfaceOptionsCheckButtonTemplate")
tableshown:SetPoint("TOPLEFT",ConFrame4,"TOPLEFT", 400,-10)
tableshown:SetChecked(PlateColorDB.auraShowDefault)
tableshown:SetScript("OnClick", function (self)
	PlateColorDB.auraShowDefault = self:GetChecked()
	AuraTableHide()
end)
local tableshownText = tableshown:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
tableshownText:SetPoint("LEFT",tableshown,"RIGHT", 5, 1)
tableshownText:SetText(L["光环显示默认列表"])

-- 创建光环表格查询添加输入框
local AuraQueryBox = ns.AddEditBox(ConFrame4,25,-8,80,true)
AuraQueryBox:HookScript("OnEnterPressed", function(self)
	self:ClearFocus() -- 清除编辑框的焦点
    local id = tonumber(self:GetText()) -- 获取编辑框中的文本
	if not id or id == "" then return end
	local idname = ns.GetSpellName(id) or L["回车添加光环名称错误"]
	--DB插入
	if type(PlateColorDB["Auras"][id]) == "table" then print(id.."--"..idname.."--"..L["回车添加光环存在"]) return end
	local newtable = {name=idname, note = "",set=true,usehp=nil,usename=nil,useshow=nil,usehide=nil,color={r=0,g=1, b=0}}
	PlateColorDB["Auras"][id] = newtable
	--表内插入
	ns.AddAuraTableButtons(AuracontentFrame,id,idname)
	C_Timer.After(0.1,function()	--延迟设置滚动条到最底部,不延迟不会获取到最大值
		AurascrollFrame:SetVerticalScroll(AurascrollFrame:GetVerticalScrollRange())
	end)
end)
AuraQueryBox:HookScript("OnTextChanged", function(self, userInput)
    local text = self:GetText() -- 获取输入框中的文本
	ns.AurayOffset = -5
	if text == "" or text == nil then
		AuraTableHide()
	else
		for key, row in pairs(ns.AurarowFrames) do
			if string.find(key,text) then
				ns.AurarowFrames[key]:Show()
			else
				ns.AurarowFrames[key]:Hide()
			end
			
			if ns.AurarowFrames[key]:IsShown() then -- 只对显示的行重新定位
            ns.AurarowFrames[key]:SetPoint("TOPLEFT", 5, ns.AurayOffset)
            ns.AurayOffset = ns.AurayOffset - 28
			end
			
		end
	end
end)

--光环添加按钮
local AuraAddButton = ns.AddButtons(ConFrame4,L["光环添加文字"],110,-11,60,23)
AuraAddButton:HookScript("OnClick", function(self, button, down)
	AuraQueryBox:ClearFocus() -- 清除编辑框的焦点
    local id = tonumber(AuraQueryBox:GetText()) -- 获取编辑框中的文本
	if not id or id == "" then return end
	local idname = ns.GetSpellName(id) or L["点击添加光环名称错误"]
	--DB插入
	if type(PlateColorDB["Auras"][id]) == "table" then print(id.."--"..idname.."--"..L["点击添加光环存在"]) return end
	local newtable = {name=idname, note = "",set=true,usehp=nil,usename=nil,useshow=nil,usehide=nil,color={r=0,g=1, b=0}}
	PlateColorDB["Auras"][id] = newtable
	--表内插入
	ns.AddAuraTableButtons(AuracontentFrame,id,idname)
	C_Timer.After(0.1,function()	--延迟设置滚动条到最底部,不延迟不会获取到最大值
		AurascrollFrame:SetVerticalScroll(AurascrollFrame:GetVerticalScrollRange())
	end)
end)
AuraAddButton:SetScript("OnEnter",function(self)
	GameTooltip:SetOwner(self, "ANCHOR_TOP") 
	GameTooltip:AddLine(L["为什么ID分不清怪插件"]) 
	GameTooltip:Show()
end)
AuraAddButton:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
end)


--恢复光环列表
local auraltextRe = ns.AddfuncButton(ConFrame4,auraltext,L["恢复光环列表"],508, 0, 125)
auraltextRe:HookScript("OnClick", function()
	ns.AurayOffset = -5
	for key, datacon in pairs(PlateColorDB["Auras"]) do
		if key then
			if ns.AurarowFrames[key] then
				ns.AurarowFrames[key]:Hide()
			end
			ns.AurarowFrames[key] = nil
		end
	end
	PlateColorDB["Auras"] = DB["Auras"]
	for key, datacon in pairs(DB["Auras"]) do
		local idname = ns.GetSpellName(key) or L["恢复光环名称错误"]
		if not datacon.color or datacon.color == 1 then
			PlateColorDB["Auras"][key]["color"] = {r=0,g=1, b=0}
		end
		ns.AddAuraTableButtons(AuracontentFrame,key,idname)
		ns.AurarowFrames[key]:Show()
	end
end)

--清空光环列表
local auraltextDe = ns.AddfuncButton(ConFrame4,auraltext,L["清空光环列表"],508,-40,125)
auraltextDe:HookScript("OnClick", function()
	PlateColorDB["Auras"] = {}
	ns.AurayOffset = -5
	for key, row in pairs(ns.AurarowFrames) do
		row:Hide()
		ns.AurarowFrames[key] = nil
	end
end)

end)